package com.yeook.ymax.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;

import com.yeook.ymax.app.ApplicationYMAX;
import com.yeook.ymax.db.Database;
import com.yeook.ymax.ui.activitys.HandlerRingtone;
import com.yeook.ymax.ui.activitys.HandlerWallaper;
import com.yeook.ymax.utils.NetworkUtils;

public class MainService  extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		registerReceiver(receiver , new IntentFilter("complete"));
		return new LocalBinder();
	}
	private BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			try{
				//serversParsing.remove(intent.getStringExtra("server"));
			}catch (Exception e){
				e.printStackTrace();
			}

		}
	};

    public class LocalBinder extends Binder{
		public MainService getService(){
			return MainService.this;
		}
	}
	protected Database db = Database.getInstance();
	
	public void loadWallaperXML()
	{
		new Thread(new Runnable() {

			@Override
			public void run() {
				File f = null;
				try {
					SAXParserFactory spf = SAXParserFactory.newInstance();
					SAXParser sp = spf.newSAXParser();
					NetworkUtils utils = new NetworkUtils();

					String url;
					String hash = "0";
                    try{
                    	hash = db.getTopHash("wallpaper");
                    }catch (Exception e) {
                    	hash = "0";
					}
					
					String fileName = utils.getFileName(hash, "wallpaper", getApplicationContext());
					
					while(fileName != null && !"".equals(fileName))
					{
						url = ApplicationYMAX.DEFAULTFILEDOWNLOAD + fileName;
						BufferedInputStream bis = new BufferedInputStream(utils.getInputStream(url, null, null, getApplicationContext()), 8 * 1024);
                        f = File.createTempFile("tempFile", "");
                        OutputStream out = new FileOutputStream(f);
                        byte buf[] = new byte[1024];
                        int len;
                        while ((len = bis.read(buf)) > 0)
                            out.write(buf, 0, len);
                        out.close();
                        bis.close();
                        
                        sp.parse(f, new HandlerWallaper());

                        f.delete();
                        try{
                        	hash = db.getTopHash("wallpaper");
            	        }catch (Exception e) {
            	        	e.printStackTrace();
            	        	break;
            			}
                        
                        getApplicationContext().sendBroadcast(new Intent("status"));
            	        
            	        fileName = utils.getFileName(hash, "wallpaper", getApplicationContext());

					}
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				} catch (SAXException e) {
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (f != null)
					f.delete();
			}
			
		}).start();
	}
	
	public void loadRingtoneXML()
	{
		new Thread(new Runnable() {

			@Override
			public void run() {
				File f = null;
				try {
					SAXParserFactory spf = SAXParserFactory.newInstance();
					SAXParser sp = spf.newSAXParser();
					NetworkUtils utils = new NetworkUtils();

					String url;
					String hash = "0";
                    try{
                    	hash = db.getTopHash("ringtone");
                    }catch (Exception e) {
                    	hash = "0";
					}
					
					String fileName = utils.getFileName(hash, "ringtone", getApplicationContext());
					
					while(fileName != null && !"".equals(fileName))
					{
						url = ApplicationYMAX.DEFAULTFILEDOWNLOAD + fileName;
						BufferedInputStream bis = new BufferedInputStream(utils.getInputStream(url, null, null, getApplicationContext()), 8 * 1024);
                        f = File.createTempFile("tempFile", "");
                        OutputStream out = new FileOutputStream(f);
                        byte buf[] = new byte[1024];
                        int len;
                        while ((len = bis.read(buf)) > 0)
                            out.write(buf, 0, len);
                        out.close();
                        bis.close();
                        
                        sp.parse(f, new HandlerRingtone());

                        f.delete();
                        
                        try{
                        	hash = db.getTopHash("ringtone");
            	        }catch (Exception e) {
            	        	break;
            			}
                        
                        getApplicationContext().sendBroadcast(new Intent("status"));
            	        
            	        fileName = utils.getFileName(hash, "ringtone", getApplicationContext());

					}
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				} catch (SAXException e) {
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (f != null)
					f.delete();
			}
			
		}).start();
	}

}
